package org.homeworkadmin.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.homeworkadmin.admin.domain.Users;
import org.homeworkadmin.admin.service.UsersService;
import org.homeworkadmin.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.homeworkadmin.common.core.controller.BaseController;


/**
 * 用户管理Controller
 *
 * @author homework
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/user")
public class UsersController extends BaseController {

    @Autowired
    private UsersService usersService;

    @GetMapping("/test")
    public AjaxResult test() {
        return new AjaxResult(200, "test");
    }

    @GetMapping("/getUsers")
    public AjaxResult getUsers(String account,String userPassword,String userName,String userPhone,
                               int pagesize,int currpage) {
        System.out.println("pagesize" + pagesize);
        System.out.println("currpage" + currpage);
        currpage = (currpage - 1) * pagesize;
        int total = usersService.getUsersCount(account,userPassword,userName,userPhone);
        List<Users> list = usersService.getUsers(account,userPassword,userName,userPhone,pagesize,currpage);
        System.out.println(list + "   " + total);
        return new AjaxResult(200, "success", list,total);
    }

    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody Users users) {
        int re = usersService.updateUser(users);
        if (re != 0){
            return success("修改用户成功");
        }else {
            return error("修改用户失败");
        }

    }

    @PostMapping("/delUser")
    public AjaxResult delUser(@RequestBody Users users) {
        int re = usersService.delUser(users.getUserId());
        if (re != 0){
            return success("删除用户成功");
        }else {
            return error("删除用户失败");
        }
    }

    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody Users users) {
        int exist = usersService.getUsersCount(users.getUserAccount(),users.getUserPassword(),
                users.getUserName(),users.getUserPhone());
        if (exist != 0){
            return error("用户已存在");
        }else {
            int re = usersService.addUser(users);
        if (re != 0){
            return success("添加用户成功");
        }else {
            return error("添加用户失败");
        }
        }

    }
}
